package com.aboo.service.impl;

import java.util.Iterator;
import java.util.List;

import com.aboo.dao.MenuDao;
import com.aboo.po.MenuPO;
import com.aboo.po.Page;
import com.aboo.pub.exception.BasicException;
import com.aboo.pub.result.ResultConstant;
import com.aboo.pub.result.ResultInfo;
import com.aboo.service.MenuService;

public class MenuServiceImpl implements MenuService {
	
	private MenuDao menuDao;

	public void saveMenu(ResultInfo ri, MenuPO param) throws BasicException {
		try {
			menuDao.save(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("保存菜单信息时出错！", ex);
		}
	}

	public void deleteMenu(ResultInfo ri, List<String> menuIds)
			throws BasicException {
		try {
			if (menuIds != null) {
				String menuId = null;
				MenuPO menu = new MenuPO();
				Integer childNum = null;
				for (Iterator<String> iter = menuIds.iterator(); iter.hasNext();) {
					menuId = iter.next();
					menu.setParentId(new Integer(menuId));
					childNum = menuDao.findCount(menu);
					if (childNum.intValue() <= 0) {
						menuDao.delete(new Integer(menuId));
					} else {
						ri.setResultCode("2");
						return;
					}					
				}
			}
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("删除菜单信息时出错！", ex);
		}
	}

	public void updateMenu(ResultInfo ri, MenuPO param) throws BasicException {
		try {
			menuDao.update(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("修改菜单信息时出错！", ex);
		}
	}
	
	public void findMenu(ResultInfo ri, MenuPO param) throws BasicException {
		try {
			MenuPO result = menuDao.find(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("修改菜单信息时出错！", ex);
		}
	}

	public void findMenuList(ResultInfo ri, MenuPO param, Page page)
			throws BasicException {
		try {
			if (page != null) {
				Integer count = menuDao.findCount(param);
				page.setTotalRecordSize(count);
			}
			
			List<MenuPO> result = menuDao.findList(param, page);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询菜单信息时出错！", ex);
		}
	}
	
	public void findMenuCount(ResultInfo ri, MenuPO param) throws BasicException {
		try {
			Integer result = menuDao.findCount(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询菜单信息时出错！", ex);
		}
	}

	public MenuDao getMenuDao() {
		return menuDao;
	}
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

}
